﻿@model Citi.GocAttributes.WebUI.Models.AutoCompleteLevelModel
<script type="text/javascript">
    var @string.Concat(Model.ControllerName, "s") = [];
    var @string.Concat(Model.ControllerName, "sId") = {};

    $(function () {
        $('#@Model.AutoCompleteInput').typeahead({
            source: function (query, process) {
                var url = '@Url.Action("GetAutocomplete", Model.ControllerName)';

                return $.getJSON(url, { searchTerm: query, level: $("#@Model.LevelInput").val() }, function (data) {
                    @string.Concat(Model.ControllerName, "s") = [];
                    @string.Concat(Model.ControllerName, "sId") = {};

                    $.each(data, function (i, item) {
                        @(string.Concat(Model.ControllerName, "sId"))[item.Description] = item;
                        @(string.Concat(Model.ControllerName, "s")).push(item.Description);
                    });

                    return process(@(string.Concat(Model.ControllerName, "s")));
                });
            }
        }).blur(function () {
            if (@(string.Concat(Model.ControllerName, "sId"))[$(this).val()] == null) {
                $('#@Model.AutoCompleteInput').val('');
                $('#@Model.HiddenIdInput').val('');
            } else {
                $('#@Model.HiddenIdInput').val(@(string.Concat(Model.ControllerName, "sId"))[$(this).val()].Id);
            }
        });
    });
</script>
@*<script type="text/javascript">
    var managedSegments = [];
    var managedSegmentsId = {};

    $(function () {
        $('#@Model.AutoCompleteInput').typeahead({
            source: function (query, process) {
                var url = '@Url.Action("GetAutocomplete", "ManagedSegment")';

                return $.getJSON(url, { searchTerm: query, LegalVehicle: $("#@Model.LegalVehicleInput").val() }, function (data) {
                    managedSegments = [];
                    managedSegmentsId = {};

                    $.each(data, function (i, item) {
                        managedSegmentsId[item.Description] = item;
                        managedSegments.push(item.Description);
                    });

                    return process(managedSegments);
                });
            }
        }).blur(function () {
            if (managedSegmentsId[$(this).val()] == null) {
                $('#@Model.AutoCompleteInput').val('');
                $('#@Model.HiddenIdInput').val('');
            } else {
                $('#@Model.HiddenIdInput').val(managedSegmentsId[$(this).val()].Id);
            }
        });
    });
</script>*@ 